package br.com.iswe.gwt.ui.client.base;

import java.util.HashMap;
import java.util.Iterator;

import br.com.iswe.gwt.ui.client.BarraTitulo;
import br.com.iswe.gwt.ui.client.comp.ISWEButton;
import br.com.iswe.gwt.ui.client.comp.LookUp;
import br.com.iswe.gwt.ui.client.comp.PropertiesBag;
import br.com.iswe.gwt.util.client.ClientUtils;
import br.com.iswe.gwt.util.client.CommonConstants;
import br.com.iswe.gwt.util.client.PropertyListener;

import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

public abstract class BaseListaLookUp extends Composite implements PropertyListener {

	//	Constantes I18N
	//protected final static CentroCustoConstants constants = (CentroCustoConstants) GWT.create(CentroCustoConstants.class);
	
	protected FlexTable layoutTable;
	protected FlexTable 			itensListTable;
	protected int       			rowItens = 1;
	protected LookUp 				lookUp;
	protected ISWEButton 			adicionarButton;
	protected PropertiesBag selectedBag = null;
	protected HashMap itens = new HashMap();
	protected String name ;
	
	public BaseListaLookUp(){
		layoutTable = new FlexTable();
		initWidget(layoutTable);
		
		//cria o layout
		lookUp = buildLookUp();
		itensListTable = new FlexTable();
		itensListTable.setWidth("100%");
		adicionarButton = new ISWEButton("Adicionar");
		
		//add Panel
		buildAddPanel();
		//Lista de Itens
		buildListItensPanel();
	}

	protected abstract LookUp buildLookUp();

	protected void buildListItensPanel() {
		//cria a linha com os cabecalhos das colunas
		itensListTable.setWidget(0, 0, ClientUtils.buildSpace(10));
		itensListTable.getFlexCellFormatter().setWidth(0, 0, "40px");
		
		itensListTable.setWidget(0, 1, new Label(getCdPropertyLabel()));
		itensListTable.getFlexCellFormatter().setWidth(0, 1, "40px");
		
		itensListTable.setWidget(0, 2, new Label(getDescPropertyLabel()));
		itensListTable.getFlexCellFormatter().setWidth(0, 2, "200px");
		
		layoutTable.setWidget(2, 0, itensListTable);
		layoutTable.getFlexCellFormatter().setColSpan(2, 0, 3);
	}

	protected void addSelectedItem(){
		//verifica se existe item para ser adicioado
		if(lookUp.getSelectedBag() == null){
			return;
		}
		String cd = lookUp.getSelectedBag().getPropriedade(getCdPropertyName());
		//verifica se ja existe 
		if(itens.containsKey(cd)){
			return;
		}
		//adiciona na lista de itens
		itens.put(cd, lookUp.getSelectedBag());
		
		//gera a lista de intens
		resetListItens();
	}
	
	private void resetListItens() {
		//limpa a tabela
		while(itensListTable.getRowCount() > 1){
			itensListTable.removeRow(1);
		}
		
		rowItens = 1;
		
		Iterator it = itens.values().iterator();
		while(it.hasNext()){
			PropertiesBag bag = (PropertiesBag)it.next();
			
			final String id = bag.getPropriedade(getCdPropertyName());
			final String nome          = bag.getPropriedade(getDescPropertyName());
			
			Hyperlink linkRemover = new Hyperlink("Remover", "Remover");
			linkRemover.addClickListener(new ClickListener(){public void onClick(Widget sender) {
				//remove a linha
				itens.remove(id);
				resetListItens();
			}});
			
			itensListTable.setWidget(rowItens, 0, linkRemover);
			itensListTable.setWidget(rowItens, 1, new Label(id));
			itensListTable.setWidget(rowItens, 2, new Label(nome));
			
			rowItens++;
		}
	}

	protected void buildAddPanel() {
		layoutTable.setWidget(0, 0, BarraTitulo.buildBarraSubArea2(getTitulo()));
		layoutTable.getFlexCellFormatter().setColSpan(0, 0, 3);
		
		layoutTable.setWidget(1, 0, (Widget)lookUp);
		layoutTable.setWidget(1, 1, ClientUtils.buildSpace(10));
		layoutTable.setWidget(1, 2, adicionarButton);
		adicionarButton.addClickListener(new ClickListener(){public void onClick(Widget sender) {
			//adiciona o item selecinado na lista
			addSelectedItem();
		}});
	}

	public LookUp getLookUp() {
		return lookUp;
	}

	protected abstract String getTitulo();

	public void setName(String name) {
		this.name = name;
	}
	public String getName() {
		return name;
	}
	
	public void clear() {
		itens.clear();
		resetListItens();
	}
	
	public String getValue() {
		if(!enabled){
			return null;
		}
		String retorno = "";
		//Mosta uma lista com os codigos dos centro de custo
		Iterator it = itens.values().iterator();
		while(it.hasNext()){
			PropertiesBag bag = (PropertiesBag)it.next();
			String cd = bag.getPropriedade(getCdPropertyName());
			retorno += cd;
			//if(it.hasNext()){//verifica se deve colocar o separador
				retorno += CommonConstants.SEPARADOR_STRING_FORM_FACTORY;
			//}
		}
		return retorno;
	}

	public void setValue(String value) {
		//Deve carregar a informacao
		
	}

	private boolean enabled = true;
	public void setEnabled(boolean enabled) {
		this.enabled = enabled; 
	}
	public boolean isEnabled(){
		return enabled;
	}
	
	protected abstract String getCdPropertyLabel();

	protected abstract String getCdPropertyName();

	protected abstract String getDescPropertyLabel();

	protected abstract String getDescPropertyName();

}
